package com.tt.fashionWebSite.controller;

import com.sun.org.apache.xpath.internal.operations.*;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.EditAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.String;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Created by isoft-work 5 on 11/16/14.
 */
@Controller
public class EditAcountController {
   @Autowired
   private EditAccountService editAccountService;


   @RequestMapping(value = "/EditAccount/getInfoUser/", method = RequestMethod.POST)
   public @ResponseBody
   JsonResponseDto<ThongTinThanhVienDto> getAllFormName(HttpServletRequest request, @RequestBody ThongTinThanhVienDto userDto) throws ParseException {
      return new JsonResponseDto<ThongTinThanhVienDto>(editAccountService.getInfoUserByEmail(userDto), JsonResponseCode.SUCCESS);
   }

   @RequestMapping(value = "/EditAccount/updateInfoUser/", method = RequestMethod.POST)
   public @ResponseBody
   JsonResponseDto<ThongTinThanhVienDto> updateInfoUser(HttpServletRequest request, @RequestBody ThongTinThanhVienDto userDto) throws ParseException {
      return new JsonResponseDto<ThongTinThanhVienDto>(editAccountService.updateInfoUser(userDto), JsonResponseCode.SUCCESS);
   }

   @RequestMapping(value = "/EditAccount/changePass/", method = RequestMethod.POST)
   public @ResponseBody
   JsonResponseDto<PhanQuyenDto> changePass(HttpServletRequest request, @RequestBody PhanQuyenDto userDto) throws ParseException {
      return new JsonResponseDto<PhanQuyenDto>(editAccountService.changePass(userDto), JsonResponseCode.SUCCESS);
   }

   @RequestMapping(value = "/EditAccount/forgetPass/", method = RequestMethod.POST)
   public @ResponseBody
   JsonResponseDto<PhanQuyenDto> forgetPass(HttpServletRequest request, @RequestBody PhanQuyenDto userDto) throws ParseException {
      return new JsonResponseDto<PhanQuyenDto>(editAccountService.forgetPass(userDto), JsonResponseCode.SUCCESS);
   }

   @RequestMapping(value = "/EditAccount/getAllBill/", method = RequestMethod.POST)
   public @ResponseBody
   JsonResponseDto<List<HoaDonDto>> getAllBillUser(HttpServletRequest request, @RequestBody ThongTinThanhVienDto userDto) throws ParseException {
      return new JsonResponseDto<List<HoaDonDto>>(editAccountService.getAllBill(userDto), JsonResponseCode.SUCCESS);
   }

   @RequestMapping(value = "/EditAccount/toDetailBill/{idHD}/{email}", method = RequestMethod.GET)
   public @ResponseBody
   void toDetailBillPage(HttpServletRequest request,HttpServletResponse response, @PathVariable String idHD, @PathVariable String email) throws ParseException {
      request.setAttribute("idHD", idHD);
      request.setAttribute("email", email);
      String url = "/jsp/user/user-check-orders.jsp";
      try {
         RequestDispatcher dispatcher = request.getRequestDispatcher(url);
         dispatcher.forward(request, response);
      }
      catch (Exception ex) {

      }
   }

   @RequestMapping(value = "/EditAccount/getDetailBill/{idHD}/{email}", method = RequestMethod.POST)
   public @ResponseBody
   JsonResponseDto<HoaDonFullDto> toDetailBillPage(HttpServletRequest request, @PathVariable String idHD, @PathVariable String email) throws ParseException {
      return new JsonResponseDto<HoaDonFullDto>(editAccountService.getDetailBill(idHD, email), JsonResponseCode.SUCCESS);
   }

   @RequestMapping(value = "/EditAccount/getDetailBillPopup/", method = RequestMethod.POST)
   public @ResponseBody
   void toDetailBillPopupPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      request.setCharacterEncoding("UTF-8");
      String email = request.getParameter("email");
      String idHD = request.getParameter("idHD");
      request.setAttribute("email", email);
      request.setAttribute("idHD", idHD);
      try {
         RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/user/user-check-bill-popup.jsp");
         dispatcher.forward(request, response);
      } catch (ServletException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   @RequestMapping(value = "/EditAccount/getDetailBillPopup/{email}/{idHD}", method = RequestMethod.POST)
   public @ResponseBody
   JsonResponseDto<HoaDonFullDto> toDetailBillPopupPage(HttpServletRequest request, @PathVariable String idHD, @PathVariable String email) throws ParseException {
      return new JsonResponseDto<HoaDonFullDto>(editAccountService.getDetailBillPopup(email, idHD), JsonResponseCode.SUCCESS);
   }
}
